using System.Collections.Generic;
using Function.SerializedPropertyAttribute;
using SCore.Util;
using UnityEditor;
using UnityEngine;

namespace SCore.Editor.BuildPackage
{
    [CustomEditor(typeof(BuildPipelineConfig))]
    public class BuildPipelineConfigEditor : ObjectDrawEditor<BuildPipelineConfig>
    {
    }

    [CreateAssetMenu(fileName = "BuildPipelineConfig", menuName = "ScriptableObject/BuildPipelineConfig", order = 1)]
    public class BuildPipelineConfig : SingleScriptableObject<BuildPipelineConfig>
    {
        public enum SBuildOp
        {
            Build = 1, //出包
            Addressable = 2, //出bundle,
            Export = 3, //导出
        }

        [Foldout("构建设置", false)] [Head("导出路径")] [DefinedValues(nameof(GetOutPutPath))] [ReadOnly]
        public string _output;

        [Foldout("构建设置", false)] [Head("编译模式")] [DefinedValues(nameof(GetTargetNames))]
        public string _release = BuildPipelineUtils.STargetType.Develop;

        [Foldout("构建设置", false)] [Head("版本")] public string _version = "1.1";

        [Foldout("构建设置", false)] [Head("时间偏移")]
        public int _timeOffset = 0;

        [Foldout("构建设置", false)] [Head("是否导出aab")]
        public bool _isExportAAb = false;


        [Foldout("构建设置", false)] [Head("bundle版本")] [Tooltip("游戏 bundle版本 变化app 会通知更新")]
        public string _bundleVersionCode = "1";


        [Foldout("构建设置", false)] [Head("sdk测试环境开关")]
        public bool _sdkEnv;

        [Foldout("构建设置", false)] [Head("注入环境")] [Tooltip("// 注入环境，，服务器名pre/pro等")]
        public string _injectEnv;

        [Foldout("构建设置", false)] [Head("firebase测试开关")]
        public bool _fireBaseTest = false;

        [Foldout("构建设置", false)] [Head("是否打开debugger")] [Tooltip("是否打开debugger, 先自动根据release初始化，再根据jenkins输入重写")]
        public bool _withDebugLog;

        [Foldout("构建设置", false)] [Head("全球SDk")] [Tooltip("是否为全球SDk")]
        public bool _isCGSDKGlobal = true;

        [Foldout("BuildIng", true)] public List<BuildChannel> _sBuildChannelMap = new List<BuildChannel>();

        public List<BuildTarget> _sBuildTargetMap = new List<BuildTarget>();

        [Head("当前构建的渠道")] [DefinedValues(nameof(GetBuildChannel))]
        public BuildChannel _buildChannel;

        [Head("当前构建的目标")] [DefinedValues(nameof(GetBuildTarget))]
        public BuildTarget _buildTarget;

        [Head("操作方式")] public SBuildOp _opExport = SBuildOp.Build;

        private BuildChannel[] GetBuildChannel()
        {
            return _sBuildChannelMap.ToArray();
        }

        private BuildTarget[] GetBuildTarget()
        {
            return _sBuildTargetMap.ToArray();
        }

        private string[] GetTargetNames()
        {
            return new[]
            {
                BuildPipelineUtils.STargetType.Develop,
                BuildPipelineUtils.STargetType.Release
            };
        }

        public BuildChannel GetSBuildChannelData(string sChannelName)
        {
            foreach (var sBuild in _sBuildChannelMap)
            {
                if (sBuild._channelName == sChannelName)
                {
                    return sBuild;
                }
            }

            $"{sChannelName} SBuildChannelData need to create".Error();
            return null;
        }

        public BuildTarget GetSBuildTargetData(string targetName)
        {
            foreach (var sBuild in _sBuildTargetMap)
            {
                if (sBuild._targetType == targetName)
                {
                    return sBuild;
                }
            }

            $"{targetName} SBuildTargetData need to create".Error();
            return null;
        }

        public string[] GetOutPutPath()
        {
            if (_buildChannel == null)
            {
                return null;
            }

            if (_isExportAAb)
            {
                return new[] { $"App/{_buildChannel._channelName}/android.aab" };
            }
            else if (_opExport == SBuildOp.Export)
            {
                return new[] { $"App/{_buildChannel._channelName}AndroidProject" };
            }

            return new[] { $"App/{_buildChannel._channelName}/android.apk" };
        }

        public void BindBuilding(BuildChannel sBuildChannel)
        {
            _buildChannel = sBuildChannel;
        }

        public void BindBuilding(BuildTarget sBuildTarget)
        {
            _buildTarget = sBuildTarget;
        }

        [ButtonMethod]
        public void SBuild()
        {
            EditorUtility.SetDirty(this);
            AssetDatabase.SaveAssets();
            BuildPackage.SBuild();
        }
    }
}